package com.atcjx.service;

import com.atcjx.MyUtils.IdUtils;
import com.atcjx.MyUtils.ResultFactory;
import com.atcjx.exception.UserException;
import com.atcjx.exception.exceptionEnum;
import com.atcjx.mapper.ArticleMapper;
import com.atcjx.mapper.TagMapper;
import com.atcjx.pojo.Article.Article;
import com.atcjx.pojo.Article.ArticleVO;
import com.atcjx.pojo.Query.Article.ArticleQueryDTO;
import com.atcjx.pojo.Query.Article.ArticleQueryPo;
import com.atcjx.pojo.Query.Tag.TagQueryPo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName TagService.java
 * @Description TODO
 * @createTime 2021年09月21日 17:47:00
 */
@Service
@Slf4j
public class TagServiceImpl  {
    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    TagMapper tagMapper;

    public List<String> getAllTags() {
        List<String> tags = tagMapper.getTagsByCondition(null);
        return tags.isEmpty()?new ArrayList<>():tags;
    }
    public  String createTag(String tag) throws UserException {
        Map<String, String> stringStringMap = new HashMap<>();
        stringStringMap.put("name",tag);
        List<String> tagsByCondition = tagMapper.getTagsByCondition(stringStringMap);
        if(!tagsByCondition.isEmpty())
            throw new UserException(exceptionEnum.SameTag.getMsg(), exceptionEnum.SameTag.getCode());
        String id = IdUtils.getRandomStringId().substring(4, 24);
        tagMapper.insertTag(id,tag);
        return ("添加"+tag+"标签成功！");
    }
    public String delete(String tag){
        tagMapper.deleteTag(tag);
        return "删除"+tag+"标签成功！";
    }

    public List<String> getTagsByTheme(String themeName) {
        return tagMapper.getTagsByThemeName(themeName);
    }
}
